package xyz.xiaolongrenzz.motorholder;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.List;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    TextView textViewX,textViewY;
    SeekBar seekBarMotorX,seekBarMotorY;
    private static final String DeviceID = "955413620";//DeviceID
    private static final String ApiKey = "nJxNvUlQyZPVKTZ13ZqMcNg=BFk=";//API KEY
    private static final String key1 = "angle_x";//onenet平台上对应设备的其中一个数据流的名字
    private static final String key2 = "angle_y";//onenet平台上对应设备的其中一个数据流的名字
    int x,y;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requestPermissions(new String[]{"android.permission.INTERNET"}, 1);

        seekBarMotorX=findViewById(R.id.seekBar_xmotor);
        seekBarMotorY=findViewById(R.id.seekBar_ymotor);
        textViewX=findViewById(R.id.textView);
        textViewY=findViewById(R.id.textView2);

        seekBarMotorX.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                textViewX.setText("X轴："+i+"°");
                x=i;
                PostX(x);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        seekBarMotorY.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                textViewY.setText("Y轴"+i+"°");
                y=i;
                PostY(y);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

    public void PostX(int i) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String update="";
                    //下发命令
                    OkHttpClient client = new OkHttpClient();
                    //数据格式从官方文档看，type用5情况
                    if(i>=10&&i<100){
                        update = ("0"+"0"+i);
                    }else if(i<10){
                        update = ("0"+"0"+"0"+i);
                    }else{
                        update = ("0"+i);
                    }
                    Log.d("xxxx", "run: "+update);
                    //发送type写法
                    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), update);
                    Request request = new Request.Builder().url("http://api.heclouds.com/cmds?device_id="+ DeviceID).post(requestBody).header("api-key", ApiKey).build();
                    Response response = client.newCall(request).execute();
                } catch (Exception e) {
                }
            }
        }).start();
    }

    public void PostY(int i) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String update="";
                    //下发命令
                    OkHttpClient client = new OkHttpClient();
                    //数据格式从官方文档看，type用5情况
                    if(i>=10&&i<100){
                        update = ("1"+"0"+i);
                    }else if(i<10){
                        update = ("1"+"0"+"0"+i);
                    }else{
                        update = ("1"+i);
                    }
                    Log.d("yyyy", "run: "+update);
                    //发送type写法
                    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), update);
                    Request request = new Request.Builder().url("http://api.heclouds.com/cmds?device_id="+ DeviceID).post(requestBody).header("api-key", ApiKey).build();
                    Response response = client.newCall(request).execute();
                } catch (Exception e) {
                }
            }
        }).start();
    }

    private void parseJSONWithGSON(String jsonData) {
        JsonRootBean app = new Gson().fromJson(jsonData, JsonRootBean.class);
        List<Datastreams> streams = app.getData().getDataStreams();
        List<DataPoints> points = streams.get(0).getDataPoints();
        int count = app.getData().getCount();//获取数据的数量
        for (int i = 0; i < points.size(); i++) {
            String time = points.get(i).getAt();
            String value = points.get(i).getValue();
            Log.w("www","time="+time);
            Log.w("www","value="+value);
        }

    }

}